package com.bbzn.device.client.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.bbzn.device.client.dao.fence.FenceDeviceMapper;
import com.bbzn.device.client.dataobject.FenceDevice;
import com.bbzn.device.client.service.FenceDeviceService;

@Service
public class FenceDeviceServiceImpl implements FenceDeviceService {

    //定义一个容量为10000的阻塞队列，BlockingQueue线程安全可以多个生产者同时put
    private BlockingQueue<FenceDevice> dataQueue = new LinkedBlockingQueue<FenceDevice>(10000);

    private List<FenceDevice> list = new ArrayList<FenceDevice>();

    @Resource
    private FenceDeviceMapper fenceDeviceMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return fenceDeviceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FenceDevice record) {
        return fenceDeviceMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(FenceDevice record) {
        return fenceDeviceMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(FenceDevice record) {
        return fenceDeviceMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(FenceDevice record) {
        return fenceDeviceMapper.insertSelective(record);
    }

    @Override
    public FenceDevice selectByPrimaryKey(Long id) {
        return fenceDeviceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FenceDevice record) {
        return fenceDeviceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FenceDevice record) {
        return fenceDeviceMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<FenceDevice> findByAll(FenceDevice fenceUser) {
        return fenceDeviceMapper.findByAll(fenceUser);
    }

    @Override
    public int updateBatch(List<FenceDevice> list) {
        return fenceDeviceMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FenceDevice> list) {
        return fenceDeviceMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FenceDevice> list) {
        return fenceDeviceMapper.batchInsert(list);
    }

    @Override
    public int findCountByAll(FenceDevice fenceUser) {
        return fenceDeviceMapper.findCountByAll(fenceUser);
    }

    @Override
    public int deleteSelective(FenceDevice fenceUser) {
        return fenceDeviceMapper.deleteSelective(fenceUser);
    }

    @Override
    public List<FenceDevice> selectByImei(String imei) {
        return fenceDeviceMapper.selectByImei(imei);
    }

    //put任务的方法，供生产者调用
    @Override
    public void recordJob(FenceDevice record) {
        try {
            dataQueue.put(record);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @PostConstruct
    @Override
    public void init() {
        Thread thread = new Thread(() -> {
            while (Boolean.TRUE) {
                FenceDevice poll = null;
                boolean pollTimeOut = false;
                long startTime;
                long endTime;
                try {
                    // poll时设置超时时间为2秒
                    poll = dataQueue.poll(2, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }

                if (null != poll) {
                    // poll到任务添加到List中
                    list.add(poll);
                } else {
                    // poll超时，设置超时标志位
                    pollTimeOut = true;
                }

                // 如果任务List等于5000或poll超时且List中还有任务就批量更新
                if (list.size() == 300 ||
                        (pollTimeOut && !CollectionUtils.isEmpty(list))) {
                    startTime = System.currentTimeMillis();
//                    updateBatch(list);
                    //带条件的批量更新
                    //带条件批量更新
                    updateBatchSelective(list);
                    list.clear();
                }
            }
        });

        thread.setName("job-batchUpdate-deamon");
        // 设置启动的线程为守护线程 直到jvm停了才停止
        thread.setDaemon(true);
        thread.start();
    }

}




